<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" label-width="68px">
      <el-form-item label="客户姓名" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户姓名"
          clearable
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="clientContact">
        <el-input
          v-model="queryParams.clientContact"
          placeholder="请输入联系方式"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-perms="['LegalCase:consulation:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="consulationList">
      <el-table-column label="咨询id" align="center" prop="id" />
      <el-table-column label="客户姓名" align="center" prop="clientName" />
      <el-table-column label="联系方式" align="center" prop="clientContact" />
      <el-table-column label="咨询内容" align="center" prop="consulationContent" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="咨询状态" align="center" prop="status">
        <template #default="scope">
          <status-tag :status="scope.row.status" :status-enum="ConsualtionStatusEnums" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-perms="['LegalCase:consulation:edit']"
          >修改</el-button>
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-perms="['LegalCase:consulation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="total>0"
      v-model:current-page="queryParams.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total"
      style="float: right"
      @change="(currentPage, pageSize) => {this.queryParams.pageNum = currentPage;this.getList()}"
    />

    <!-- 添加或修改委托咨询对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户姓名" prop="clientName">
          <el-input v-model="form.clientName" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="clientContact">
          <el-input v-model="form.clientContact" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="咨询内容" prop="consulationContent">
          <el-input type="textarea" v-model="form.consulationContent" placeholder="请输入咨询内容" />
        </el-form-item>
        <el-form-item label="咨询状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="item in convertToOptions(ConsualtionStatusEnums)"
              :key="item.value"
              :label="item.label"
              :value="item.value.toString()"
            >{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listConsulation, getConsulation, delConsulation, addConsulation, updateConsulation } from "@/api/LegalCase/consulation";
import StatusTag from '@/components/StatusTag.vue'
import { ConsualtionStatusEnums } from '@/enum.js'
import { convertToOptions } from '@/utils/util.js'

export default {
  name: "Consulation",
  computed: {
    ConsualtionStatusEnums() {
      return ConsualtionStatusEnums
    }
  },
  components: { StatusTag },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 委托咨询表格数据
      consulationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientName: null,
        clientContact: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clientName: [
          { required: true, message: "客户姓名不能为空", trigger: "blur" }
        ],
        clientContact: [
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
        consulationContent: [
          { required: true, message: "咨询内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    convertToOptions,
    /** 查询委托咨询列表 */
    getList() {
      this.loading = true;
      listConsulation(this.queryParams).then(response => {
        this.consulationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        clientName: null,
        clientContact: null,
        consulationContent: null,
        status: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加委托咨询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConsulation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改委托咨询";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConsulation(this.form).then(response => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsulation(this.form).then(response => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除委托咨询编号为"' + ids + '"的数据项？').then(function() {
        return delConsulation(ids);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
